package net.huashitong.common.controller;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.common.entity.SysMessageTemplate;
import net.huashitong.common.filter.SysMessageTemplateFilter;
import net.huashitong.common.service.SysMessageTemplateService;
import net.huashitong.util.UtilsTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 短信模板管理
 * @author
 */
@Controller
@RequestMapping(value = "/common/smsTemplate")
public class SysMessageTemplateController extends JsonBaseController {
    @Autowired
    private SysMessageTemplateService sysMessageTemplateService;
    @Override
    protected String getView(String s) {
        return "/modules/common/smsTemplate/"+s;
    }


    @RequestMapping("/filter")
    public String filter(@ModelAttribute SysMessageTemplateFilter filter) {
        return getView("filter");
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public JqGrid iniPageData(SysMessageTemplateFilter filter) {
        List<SysMessageTemplate> templateList = sysMessageTemplateService.queryByFilter(filter);
        return coverJqGrid(filter,templateList);
    }

    @RequestMapping(method = RequestMethod.GET)
    public String initPage(Model model) {
        return getView("query");
    }



    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String showEditView(Model model, @PathVariable String id) {
        SysMessageTemplate template = sysMessageTemplateService.getById(id);
        model.addAttribute("SysMessageTemplateFilter",template);
        return getView("form");
    }

    @ResponseBody
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Message update(@ModelAttribute SysMessageTemplateFilter filter) {
        if (StringUtils.isBlank(filter.getSysId())) {
            return coverMessage(UtilsTools.ERROR,"模板不存在");
        }
        SysMessageTemplate template = new SysMessageTemplate();
        UtilsTools.copyPro(filter,template);
        sysMessageTemplateService.update(template);
        return coverMessage(UtilsTools.CODE);
    }

    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String showAddView(@ModelAttribute SysMessageTemplateFilter filter , Model model) {
        model.addAttribute("SysMessageTemplateFilter",filter);
        return getView("form");
    }
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Message create(@ModelAttribute SysMessageTemplateFilter filter) {
        SysMessageTemplate sysMessage = new SysMessageTemplate();
        UtilsTools.copyPro(filter,sysMessage);
        sysMessageTemplateService.save(sysMessage);
        return coverMessage(UtilsTools.CODE);
    }


    @ResponseBody
    @RequestMapping(value = "/delete/{id}")
    public Message deleteTemplate(@PathVariable List<String> id){
        sysMessageTemplateService.batchDelete(id);
        return coverMessage(UtilsTools.CODE);
    }

    @ResponseBody
    @RequestMapping(value = "/check/{code}",method = RequestMethod.GET)
    public Message checkTemplate(@PathVariable String code){
        SysMessageTemplateFilter filter =new SysMessageTemplateFilter();
        filter.setTemplateCode(code);
        List<SysMessageTemplate> templateList = sysMessageTemplateService.queryByFilter(filter);
        if (templateList == null||templateList.size()==0){
            return coverMessage(UtilsTools.CODE);
        } else {
            return coverMessage(UtilsTools.ERROR);
        }

    }
}
